package com.colourclash.fruitopia.utils
{
	import com.colourclash.fruitopia.events.InitialisedEvent;
	import flash.events.EventDispatcher;
	
	/**
	 * ...
	 * @author Patrick
	 */
	public class BaseController extends EventDispatcher
	{
		private var initialised:Boolean;
		
		public var id:String;
		
		protected var model:BaseModel;
		
		public function BaseController(model:BaseModel)
		{
			this.model = model;
			
			id = model.id;
			
			Global.getInstance().stage.addEventListener(InitialisedEvent.VIEW_INITIALISED, initialiseEventHandler, false, 0, true);
		}
		
		private function initialiseEventHandler(e:InitialisedEvent):void
		{
			Global.getInstance().stage.removeEventListener(InitialisedEvent.VIEW_INITIALISED, initialiseEventHandler);
			
			if (e.id == model.id && !initialised)
			{
				initialised = true;
				initialise();
			}
		}
		
		public function initialise():void
		{
			//override me
		}
	}

}